package 力扣._00周赛._426_周赛.问题2;

import java.util.HashMap;

class Solution {
    public int getLargestOutlier(int[] nums) {
        int max = Integer.MIN_VALUE;
        int totalSum = 0;

        // 计算所有数字的总和
        for (int num : nums) {
            totalSum += num;
        }

        // 使用 HashMap 存储每个数字的出现次数
        HashMap<Integer, Integer> numCountMap = new HashMap<>();
        for (int num : nums) {
            numCountMap.put(num, numCountMap.getOrDefault(num, 0) + 1);
        }

        // 遍历每个数字，检查是否满足条件
        for (int num : nums) {
            int sumWithoutCurrent = totalSum - num;
            if (sumWithoutCurrent % 2 == 0) {
                int halfSum = sumWithoutCurrent / 2;
                if (numCountMap.containsKey(halfSum)) {
                    if(halfSum == num) {
                        if (numCountMap.get(halfSum) > 1) {
                            max = Math.max(max, num);
                        }
                    } else {
                        max = Math.max(max, num);
                    }
                }
            }
        }

        return max;
    }
}